<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


/**
 * 个人中心接口
 */
class My extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
        $this->model = new \app\common\model\Order;
    }  
    /**
     * 我的余额情况列表status状态：1=收入，2=支出
     */
    public function yue_list(){
      $return = Db::name('yu_log as y')->join('shop s','y.shop_id=s.id')->field("y.*,s.name as shop_name,FROM_UNIXTIME(y.createtime,'%Y-%m-%d %H:%i:%s') as createtime")->where(['user_id'=>$this->user_id])->select();
      $this->success('我的余额情况列表',$return);
    } 
    
    /**
     * 余额提现
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/my/yue_tixian)
     * @ApiParams   (name="money",type=decimal,required=true,description="提现值")
     * @ApiReturn({

     })
     */
    public function yue_tixian(){
      $money = input('money');

      $my_money = $this->auth->money;//个人话费

      if($my_money<$money)$this->error('余额余额不足已兑换');
      if($money<10)$this->error('至少10元起提现');

      $insert_yue_log['text'] = '余额提现'.$money;
      $insert_yue_log['user_id'] = $this->user_id;
      $insert_yue_log['createtime'] = time();
      $insert_yue_log['price'] = $money;
      $insert_yue_log['status'] = 2;

      Db::name('yu_log')->insert($insert_yue_log);//余额记录
      Db::name('user')->where('id',$this->user_id)->setDec('money',$money);
      $this->success('兑换成功');
    } 
    /**
     * 我的订单 未支付，代发货，已发货，已确认，邮寄到家
     */
    public function order_list(){
      // pay_status 支付状态:10=未支付,20=已支付,30=已发货,40=确认收货
      $return['weizhifu'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime")->where(['user_id'=>$this->user_id,'pay_status'=>10])->select();

      $return['daifahuo'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime")->where(['user_id'=>$this->user_id,'pay_status'=>20])->select();

      $return['yifahuo'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,FROM_UNIXTIME(delivery_time,'%Y-%m-%d %H:%i:%s') as delivery_time")->where(['user_id'=>$this->user_id,'pay_status'=>30])->select();

      $return['queren'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,FROM_UNIXTIME(delivery_time,'%Y-%m-%d %H:%i:%s') as delivery_time")->where(['user_id'=>$this->user_id,'pay_status'=>40])->select();
      $return['youjidaojia'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,FROM_UNIXTIME(delivery_time,'%Y-%m-%d %H:%i:%s') as delivery_time")->where(['user_id'=>$this->user_id,'pay_status'=>50])->select();
      $this->success('我的订单',$return);
    }

    /**
     * 确认收货
     * @param int $order_id
     */
    public function sure_get_order(){
      $order_id = input('order_id');

      $my_order_data = $this->model->where('id',$order_id)->find();
      if(empty($my_order_data))$this->error('该订单信息不存在');

      if($my_order_data['pay_status']=='40')$this->error('该订单已经确认过收货了');

      $my_order_data->isUpdate(true)->save(['pay_status'=>40]);

      $this->success('确认收货成功');
    }

    

  

}
